package prosjekt;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import javax.swing.*;

/**
 * Information panel for delivery person
 * @author Henning
 *
 */
@SuppressWarnings("serial")
public class LeveringsInfoForm extends JPanel {
	ResultSet brukerInfo;
	Vector<String> vareVektor;
	String fornavn, etternavn, adr, poststed, husnr, tlf = "";
	int postnr = 0;
	int aktivOrdre = 0;
	/**
	 * Default constructor
	 * @param i OrdreID
	 * @throws SQLException
	 */
	public LeveringsInfoForm(int i) throws SQLException {
		super(new BorderLayout());
		aktivOrdre = i;
		vareVektor = RestaurantDB.getVareInfo(i);
		brukerInfo = RestaurantDB.getKundeInfo(i);

		while (brukerInfo.next()) {
			adr = brukerInfo.getString("kundeinfo.adresse");
			poststed = brukerInfo.getString("kundeinfo.poststed");
			postnr = brukerInfo.getInt("kundeinfo.postnr");
			fornavn = brukerInfo.getString("kundeinfo.fornavn");
			etternavn = brukerInfo.getString("kundeinfo.etternavn");
			husnr = brukerInfo.getString("kundeinfo.husnr");
			tlf = brukerInfo.getString("kundeinfo.tlfnr");
		}

		add(new InfoPanel(), BorderLayout.CENTER);

		add(new KartPanel(adr+" "+husnr+" "+poststed), BorderLayout.LINE_END);
	}
	
	/**
	 * Panel containing order and customer information
	 * @author Henning
	 *
	 */
	class InfoPanel extends JPanel {
		/**
		 * Default constructor
		 * @throws SQLException
		 */
		public InfoPanel() throws SQLException {
			super();
			BoxLayout layout = new BoxLayout(this, BoxLayout.Y_AXIS);
			setLayout(layout);
			
			setBorder(BorderFactory.createTitledBorder("Ordreinformasjon"));
			
			JTextField navn = new JTextField("Navn:\t"+fornavn+" "+etternavn);
			navn.setMaximumSize(new Dimension(1000, 30));
			JTextField adresse = new JTextField("Adresse:\t"+adr+" "+husnr);
			adresse.setMaximumSize(new Dimension(1000, 30));
			JTextField post = new JTextField("Poststed:\t"+postnr+" "+poststed);
			post.setMaximumSize(new Dimension(1000, 30));
			JTextField telefon = new JTextField("Tlf:\t"+tlf);
			telefon.setMaximumSize(new Dimension(1000, 30));
			
			JList vareListe = new JList(vareVektor);
			vareListe.setMaximumSize(new Dimension(1000, 3000));
			
			JButton levert = new JButton("Ordre levert");
			levert.setMaximumSize(new Dimension(200, 40));
			levert.addActionListener(new FerdigKlikk());
			
			add(navn);
			add(Box.createVerticalStrut(3));
			add(adresse);
			add(Box.createVerticalStrut(3));
			add(post);
			add(Box.createVerticalStrut(3));
			add(telefon);
			add(Box.createVerticalStrut(6));
			add(vareListe);
			add(levert);
		}
		
		/**
		 * Listener for "Ordre levert" button
		 * @author Henning
		 *
		 */
		class FerdigKlikk implements ActionListener {
			public void actionPerformed(ActionEvent e) {
				try {
					RestaurantDB.setOrdreLevert(aktivOrdre);
				} catch (SQLException e1) {
					e1.printStackTrace();
				}
			}
		}
	}
	
	/**
	 * Panel containing maps zoomed to the customers address
	 * @author Henning
	 *
	 */
	class KartPanel extends JPanel {
		public KartPanel(String levAdr) {
			super(new GridLayout(2,1));
			setBorder(BorderFactory.createTitledBorder("Kart"));
			GMapLabel farMap = new GMapLabel(12, levAdr);
			GMapLabel closeMap = new GMapLabel(14, levAdr);
			add(farMap);
			add(closeMap);
		}
	}
}
